{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/nceder/qpb4e/blob/main/code/Chapter%2008/Chapter_08.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# 8 Control flow\n",
        "\n",
        "# 8.1 The if-elif-else statement"
      ],
      "metadata": {
        "id": "DcQVEBBoWN35"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# you can change the value of guess to test how the if statement behaves\n",
        "guess = 4\n",
        "target = 7\n",
        "\n",
        "if guess < target:\n",
        "  print(\"Low\")\n",
        "elif guess == target:\n",
        "  print(\"Correct!\")\n",
        "else:\n",
        "  print(\"High\")"
      ],
      "metadata": {
        "id": "WD3ANJ-CdN1p",
        "outputId": "59f46fc1-cec6-4837-a7bb-1fb8b8c984f7",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Low\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# 8.2 Structural pattern matching with match"
      ],
      "metadata": {
        "id": "sTylNCpIWjOz"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# change the value of x to experiment with the match statement\n",
        "x = 5\n",
        "match x:\n",
        "  case \"A\":\n",
        "    print(\"A\")\n",
        "  case str():\n",
        "    print(\"some other string\")\n",
        "  case 0:\n",
        "    print(\"Zero\")\n",
        "  case 1 | 2 | 3:\n",
        "    print(f\"value {x} in range\")\n",
        "  case int():\n",
        "    print(f\"The integer {x}\")\n",
        "  case _:\n",
        "    print(\"Neither string nor int\")\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "xra3XGgNXFHD",
        "outputId": "a3b7a456-5014-4a08-e60a-e7c1af3f668d"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "The integer 5\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# 8.3 The while loop"
      ],
      "metadata": {
        "id": "Yo7jTv_uWpWk"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "response = \"\"\n",
        "while response != \"Q\":\n",
        "  response = input(\"Q to quit, B to break\")\n",
        "  if response == \"B\":\n",
        "    break\n",
        "else:\n",
        "  print(\"no break\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "it8wan-nXF5B",
        "outputId": "e1b3696a-9b8a-49f4-bde1-b8e6501b7c81"
      },
      "execution_count": null,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Q to quit, B to breakl\n",
            "Q to quit, B to breakB\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# 8.4 The for loop"
      ],
      "metadata": {
        "id": "AGiiio1bWwNL"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "x = [1.0, 2.0, 3.0, 0.0, -1.0]\n",
        "for n in x:\n",
        "    if n == 0.0:\n",
        "      print(\"skipping 0.0\")\n",
        "      continue\n",
        "    elif n < 0.0:\n",
        "      print(\"no negative numbers!\")\n",
        "      break\n",
        "    print(1 / n)\n",
        "else:\n",
        "  print(\"Loop completed normally\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "lsOs39wgXJa0",
        "outputId": "0250c529-692d-4d30-d3b6-09520e80bbea"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "1.0\n",
            "0.5\n",
            "0.3333333333333333\n",
            "skipping 0.0\n",
            "no negative numbers!\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 8.4.1 The range function"
      ],
      "metadata": {
        "id": "6XYipsZ7Wpaz"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "x = [1, 3, -7, 4, 9, -5, 4]\n",
        "for i in range(len(x)):\n",
        "    if x[i] < 0:\n",
        "        print(\"Found a negative number at index \", i)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "mhpcS5-NXSjs",
        "outputId": "fe3748f3-8cb1-479b-e3c8-69a6ba18878b"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Found a negative number at index  2\n",
            "Found a negative number at index  5\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 8.4.2 Controlling range with starting and stepping values"
      ],
      "metadata": {
        "id": "1QI-lFM9WD2N"
      }
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Kzu2rpFZm6fN",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "ec9e6188-a892-4297-dde4-c19b3d957510"
      },
      "source": [
        "list(range(3, 7))        #A"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[3, 4, 5, 6]"
            ]
          },
          "metadata": {},
          "execution_count": 7
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HzEE-ZO0m6fR",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "eb82190b-2c62-4aab-9c10-b430450d3dd0"
      },
      "source": [
        "list(range(2, 10))       #A"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[2, 3, 4, 5, 6, 7, 8, 9]"
            ]
          },
          "metadata": {},
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "J7JSxvtJm6fR",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "59a22453-fbe2-4492-eda8-b8e9ec515467"
      },
      "source": [
        "list(range(5, 3))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[]"
            ]
          },
          "metadata": {},
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "MWBzkiyUm6fS",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "f51a8378-4bda-49ca-a489-4a0d041c8189"
      },
      "source": [
        "list(range(0, 10, 2))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[0, 2, 4, 6, 8]"
            ]
          },
          "metadata": {},
          "execution_count": 10
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "kk0REzCRm6fS",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "5f6a9259-4b83-4a03-ed0a-77519b4b0601"
      },
      "source": [
        "list(range(5, 0, -1))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[5, 4, 3, 2, 1]"
            ]
          },
          "metadata": {},
          "execution_count": 11
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 8.4.3 Using break and continue in for loops"
      ],
      "metadata": {
        "id": "KIZ_l_m0XcMg"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 8.4.4 The for loop and tuple unpacking"
      ],
      "metadata": {
        "id": "cgoeLUDrXqC_"
      }
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "H4a_aQTym6fT"
      },
      "source": [
        "somelist = [(1, 2), (3, 7), (9, 5)]\n",
        "result = 0\n",
        "for t in somelist:\n",
        "    result = result + (t[0] * t[1])"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "sqddire9m6fT"
      },
      "source": [
        "somelist = [(1, 2), (3, 7), (9, 5)]\n",
        "result = 0\n",
        "\n",
        "for x, y in somelist:\n",
        "    result = result + (x * y)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 8.4.5 The enumerate function"
      ],
      "metadata": {
        "id": "PW2UTBe-Xv5s"
      }
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JHSRaLW0m6fU",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "a5b2cb66-3203-4bb1-b761-3b22270e9dc3"
      },
      "source": [
        "x = [1, 3, -7, 4, 9, -5, 4]\n",
        "for i, n in enumerate(x):             #A\n",
        "    if n < 0:                                #B\n",
        "        print(\"Found a negative number at index \", i)   #C"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Found a negative number at index  2\n",
            "Found a negative number at index  5\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 8.4.6 The zip function"
      ],
      "metadata": {
        "id": "972Cgs2uX2sT"
      }
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "t-BEQb51m6fV",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "6835c1b4-f32a-4980-d7da-b48016b12748"
      },
      "source": [
        "x = [1, 2, 3, 4]\n",
        "y = ['a', 'b', 'c']         #A\n",
        "z = zip(x, y)\n",
        "list(z)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[(1, 'a'), (2, 'b'), (3, 'c')]"
            ]
          },
          "metadata": {},
          "execution_count": 15
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Try This: Looping and if statements\n",
        "Suppose that you have a list `x = [1, 3, 5, 0, -1, 3, -2]`, and you need to remove all negative numbers from that list. Write the code to do this.\n",
        "\n",
        "How would you count the total number of negative numbers in a list `y = [[1, -1, 0], [2, 5, -9], [-2, -3, 0]]`?\n",
        "\n",
        "What code would you use to print \"very low\" if the value of x is below -5, \"low\" if it's from -5 up to 0, \"neutral\" if it's equal to 0, \"high\" if it'' greater than 0 up to 5, and \"very high\" if it's greater than 5?\n"
      ],
      "metadata": {
        "id": "Kb67dQT3X9Zv"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Suppose that you have a list `x = [1, 3, 5, 0, -1, 3, -2]`, and you need to remove all negative numbers from that list. Write the code to do this."
      ],
      "metadata": {
        "id": "b3OTX78eY5IV"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "x = [1, 3, 5, 0, -1, 3, -2]\n",
        "for i in x:\n",
        "    if i < 0:\n",
        "        x.remove(i)\n",
        "print(x)\n"
      ],
      "metadata": {
        "id": "DvfGD6IjYz-g"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "How would you count the total number of negative numbers in a list `y = [[1, -1, 0], [2, 5, -9], [-2, -3, 0]]`?"
      ],
      "metadata": {
        "id": "N2svf1vJY7Dn"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "count = 0\n",
        "y = [[1, -1, 0], [2, 5, -9], [-2, -3, 0]]\n",
        "for row in y:\n",
        "    for col in row:\n",
        "        if col < 0:\n",
        "            count += 1\n",
        "print(count)\n"
      ],
      "metadata": {
        "id": "1NeP2pUDYxjj"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "What code would you use to print \"very low\" if the value of x is below -5, \"low\" if it's from -5 up to 0, \"neutral\" if it's equal to 0, \"high\" if it'' greater than 0 up to 5, and \"very high\" if it's greater than 5?\n"
      ],
      "metadata": {
        "id": "VtViyECSZJaE"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "if x < -5:\n",
        "    print(\"very low\")\n",
        "elif x <= 0:\n",
        "    print(\"low\")\n",
        "elif x == 0:   # could be 'elif not x:' but the point here is comparison to 0\n",
        "    print(\"neutral\")\n",
        "elif x <= 5:\n",
        "    print(\"high\")\n",
        "else:\n",
        "    print(\"very high\")"
      ],
      "metadata": {
        "id": "c3pTlr55Yqkf"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "# 8.5 List, set, and dictionary comprehensions"
      ],
      "metadata": {
        "id": "qEfUJ7PgZVGz"
      }
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "X0-wREDbm6fV",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "8e441b1b-5d39-4569-f622-a229877a0625"
      },
      "source": [
        "x = [1, 2, 3, 4]\n",
        "x_squared = []\n",
        "for item in x:\n",
        "    x_squared.append(item * item)\n",
        "\n",
        "x_squared"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[1, 4, 9, 16]"
            ]
          },
          "metadata": {},
          "execution_count": 16
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "tHjNpfvpm6fW",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "f7b08b02-885d-4b45-826d-e3c9509ef491"
      },
      "source": [
        "x = [1, 2, 3, 4]\n",
        "x_squared = [item * item for item in x]\n",
        "x_squared"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[1, 4, 9, 16]"
            ]
          },
          "metadata": {},
          "execution_count": 17
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "yxTeBv8jm6fW"
      },
      "source": [
        "x = [1, 2, 3, 4]\n",
        "x_squared = [item * item for item in x if item > 2]"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4ceEO1otm6fW",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "ff0d7d89-2c38-4e88-d999-2a2f2a2637f5"
      },
      "source": [
        "x_squared"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[9, 16]"
            ]
          },
          "metadata": {},
          "execution_count": 19
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "lddRmrZ6m6fW",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "fce652f2-db87-45d1-a7e3-f8e27569fa1b"
      },
      "source": [
        "x = [1, 2, 3, 4]\n",
        "x_squared_dict = {item: item * item for item in x}\n",
        "x_squared_dict"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "{1: 1, 2: 4, 3: 9, 4: 16}"
            ]
          },
          "metadata": {},
          "execution_count": 20
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 8.5.1 Generator expressions"
      ],
      "metadata": {
        "id": "v_9-PiyIZaXt"
      }
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ZVZnoxljm6fX",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "6f23dc85-f709-415b-d0fb-12368a44df21"
      },
      "source": [
        "x = [1, 2, 3, 4]\n",
        "x_squared = (item * item for item in x)\n",
        "x_squared"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<generator object <genexpr> at 0x7ae74cd5d770>"
            ]
          },
          "metadata": {},
          "execution_count": 21
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "EiameIGBm6fX",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "77143459-d79f-48d9-99d0-6552009189cf"
      },
      "source": [
        "for square in x_squared:\n",
        "    print(square,)\n"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "1\n",
            "4\n",
            "9\n",
            "16\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Try This: Comprehensions\n",
        "What list comprehension would you use to process the list x so that all negative values are removed?\n",
        "\n",
        "Create a generator that returns only odd numbers from 1 to 100. (Hint: A number is odd if there is a remainder if divided by 2; use % 2 to get the remainder of division by 2.)\n",
        "\n",
        "Write the code to create a dictionary of the numbers and their cubes from 11 through 15.\n"
      ],
      "metadata": {
        "id": "5Ea383d3Ze_L"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "What list comprehension would you use to process the list x so that all negative values are removed?"
      ],
      "metadata": {
        "id": "48hX5XpFZqLa"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "x = [1, 3, 5, 0, -1, 3, -2]\n",
        "new_x = [i for i in x if i >= 0]\n",
        "print(new_x)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "pqKkgzWwZslR",
        "outputId": "dabdc9eb-ba7d-4284-b9b4-0d34f994d03f"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[1, 3, 5, 0, 3]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Create a generator that returns only odd numbers from 1 to 100. (Hint: A number is odd if there is a remainder if divided by 2; use % 2 to get the remainder of division by 2.)"
      ],
      "metadata": {
        "id": "CJ8LM5q9Zs6f"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "odd_100 = (x for x in range(100) if x % 2)\n",
        "for i in odd_100:\n",
        "    print(i))"
      ],
      "metadata": {
        "id": "5vQItQoMZu6g"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Write the code to create a dictionary of the numbers and their cubes from 11 through 15."
      ],
      "metadata": {
        "id": "iGEHBWmDZvbd"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "cubes = {x: x**3 for x in range(11, 16)}\n",
        "print(cubes)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ILWo6RiPZxev",
        "outputId": "cf5820a9-b22d-4930-e7f2-d2346ed8818b"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{11: 1331, 12: 1728, 13: 2197, 14: 2744, 15: 3375}\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# 8.6 Statements, blocks, and indentation"
      ],
      "metadata": {
        "id": "oNKtMlG3aJcg"
      }
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ka_4uqLUm6fX",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "d99a2bc4-396a-4026-9007-f4fd2c2717c2"
      },
      "source": [
        "x = 1; y = 0; z = 0\n",
        "if x > 0: y = 1; z = 10\n",
        "else: y = -1\n",
        "\n",
        "print(x, y, z)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "1 1 10\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mStGpWhbm6fX",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 106
        },
        "outputId": "8847e151-a69b-46c9-dee0-17519ac69f00"
      },
      "source": [
        "x = 1\n",
        " x = 2"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "error",
          "ename": "IndentationError",
          "evalue": "unexpected indent (<ipython-input-24-c75cba843d9b>, line 2)",
          "traceback": [
            "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-24-c75cba843d9b>\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m    x = 2\u001b[0m\n\u001b[0m    ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unexpected indent\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "MZqrwUArm6fX"
      },
      "source": [
        ">>> x = 1\n",
        "... if x == 1:\n",
        "...    y = 2\n",
        "...    if y > 0:\n",
        "...        z = 2\n",
        "...        v = 0\n",
        "...\n",
        ">>> x = 2"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "lzMPhBVDm6fX",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 106
        },
        "outputId": "23e8b474-b609-4b85-b56c-c269c7e67584"
      },
      "source": [
        "x = 1\n",
        "if x == 1:\n",
        "    y = 2\n",
        "  z = 2"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "error",
          "ename": "IndentationError",
          "evalue": "unindent does not match any outer indentation level (<tokenize>, line 4)",
          "traceback": [
            "\u001b[0;36m  File \u001b[0;32m\"<tokenize>\"\u001b[0;36m, line \u001b[0;32m4\u001b[0m\n\u001b[0;31m    z = 2\u001b[0m\n\u001b[0m    ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unindent does not match any outer indentation level\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qvwJnd-am6fY",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "1b0b9eeb-6159-48ac-9b3e-1a923aa2188c"
      },
      "source": [
        "print('string1', 'string2', 'string3' \\\n",
        "   , 'string4', 'string5')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "string1 string2 string3 string4 string5\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "u-3HvGoAm6fY",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "750b131c-c2ec-4f61-be0f-5ccd222965c8"
      },
      "source": [
        "x = 100 + 200 + 300 \\\n",
        "   + 400 + 500\n",
        "x"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1500"
            ]
          },
          "metadata": {},
          "execution_count": 29
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hai-minvm6fY",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "69fceb99-3953-4ae6-c829-f82f84cf388f"
      },
      "source": [
        "v = [100, 300, 500, 700, 900,\n",
        "   1100, 1300]\n",
        "v"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[100, 300, 500, 700, 900, 1100, 1300]"
            ]
          },
          "metadata": {},
          "execution_count": 30
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "o_AdsDLom6fY",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "5c5a142c-344a-419a-979f-fdaf2eb8a82b"
      },
      "source": [
        "max(1000, 300, 500,\n",
        "       800, 1200)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1200"
            ]
          },
          "metadata": {},
          "execution_count": 31
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "NNSyC8Sym6fY",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "d9df4c6f-81c1-4619-d12e-511ed4207ff3"
      },
      "source": [
        "x = (100 + 200 + 300\n",
        "         + 400 + 500)\n",
        "x"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1500"
            ]
          },
          "metadata": {},
          "execution_count": 32
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "DJGrWtgnm6fY",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "c250e915-7c7a-4b68-cd95-4446caa9436a"
      },
      "source": [
        "\"strings separated by whitespace \"    \\\n",
        "   '''are automatically'''  ' concatenated'"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'strings separated by whitespace are automatically concatenated'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 33
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "FtbGXPa5m6fZ",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "e913a27c-7f1a-42a2-e5ed-842bdcdc0502"
      },
      "source": [
        "x = 1\n",
        "if x > 0:\n",
        "       string1 = \"this string broken by a backslash will end up \\\n",
        "               with the indentation tabs in it\"\n",
        "\n",
        "repr(string1)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\"'this string broken by a backslash will end up                with the indentation tabs in it'\""
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 34
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "a-9g4vDCm6fZ",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "8b310f22-e8bd-45e7-ac49-c21e3172532e"
      },
      "source": [
        "if x > 0:\n",
        "       string1 = \"this can be easily avoided by splitting the \" \\\n",
        "           \"string in this way\"\n",
        "\n",
        "string1"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'this can be easily avoided by splitting the string in this way'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 35
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# 8.7 Boolean values and expressions\n",
        "\n",
        "## 8.7.2 Comparison and Boolean operators"
      ],
      "metadata": {
        "id": "IIhDrKEaaVTH"
      }
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3cWdPZr0m6fZ",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "90b531e7-adb7-4f33-a905-3e510b826fc9"
      },
      "source": [
        "[2] and [3, 4]"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[3, 4]"
            ]
          },
          "metadata": {},
          "execution_count": 36
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xzTfYgB3m6fa",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "8735eebc-68a3-4b22-d6f6-f3109b45252e"
      },
      "source": [
        "[] and 5"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[]"
            ]
          },
          "metadata": {},
          "execution_count": 37
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "L8BhDIJvm6fa",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "2a84bc33-8870-4a51-b6ca-938466696733"
      },
      "source": [
        "[2] or [3, 4]"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[2]"
            ]
          },
          "metadata": {},
          "execution_count": 38
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "AvI60GFLm6fa",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "df808348-c0e5-4437-bf16-c34349646dc4"
      },
      "source": [
        "[] or 5"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "5"
            ]
          },
          "metadata": {},
          "execution_count": 39
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "X1VjUhTTm6fa",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "65e1c6de-a549-4739-c247-8799a31f49ce"
      },
      "source": [
        "x = [0]\n",
        "y = [x, 1]\n",
        "x is y[0]          #A"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {},
          "execution_count": 40
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "NAQost6Um6fa",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "ee608725-17d6-4a0e-a98c-dd50c0340445"
      },
      "source": [
        "x = [0]            #B\n",
        "x is y[0]"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "False"
            ]
          },
          "metadata": {},
          "execution_count": 41
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "H2YQ2oOVm6fa",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "9885d45b-1301-480a-ba65-cf527be3e545"
      },
      "source": [
        "x == y[0]"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {},
          "execution_count": 42
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Quick Check: Booleans and truthiness\n",
        "Decide whether the following statements are true or false: `1`, `0`, `-1`, `[0]`, `1 and 0`, `1 > 0 or []`."
      ],
      "metadata": {
        "id": "W30gPjjvalYs"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "* `1` ->: `True`.\n",
        "* `0` ->: `False`.\n",
        "* `-1`: `True`.\n",
        "* `[0]`: `True`; it's a list containing one item.\n",
        "* `1 and 0`: False.\n",
        "* `1 > 0 or []`: True.\n"
      ],
      "metadata": {
        "id": "PJ_lmPRzbxbE"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# 8.8 Writing a simple program to analyze a text file\n",
        "\n",
        "**Execute the cell below to create the file `word_count.tst`**"
      ],
      "metadata": {
        "id": "BSMrRXKYavg_"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# create file word_count.tst\"\n",
        "open(\"word_count.tst\", \"w\").write(\"\"\"Python provides a complete set of control flow elements,\n",
        "including while and for loops, and conditionals.\n",
        "Python uses the level of indentation to group blocks\n",
        "of code with control elements.\"\"\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "AqgSemcesET6",
        "outputId": "7cfabda2-6308-45e1-c3c9-f858d03b5363"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "189"
            ]
          },
          "metadata": {},
          "execution_count": 43
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vaCk3xcQm6fb",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "6957e561-d60d-463e-925c-20720c38f97b"
      },
      "source": [
        "#!/usr/bin/env python3\n",
        "\n",
        "''' Reads a file and returns the number of lines, words,\n",
        "    and characters - similar to the UNIX wc utility\n",
        "'''\n",
        "\n",
        "infile = open('word_count.tst')                   #A\n",
        "\n",
        "lines = infile.read().split(\"\\n\")          #B\n",
        "\n",
        "line_count = len(lines)               #C\n",
        "\n",
        "word_count = 0     #D\n",
        "char_count = 0     #D\n",
        "\n",
        "for line in lines:                      #E\n",
        "\n",
        "    words = line.split()                #F\n",
        "    word_count += len(words)\n",
        "\n",
        "    char_count += len(line)             #G\n",
        "\n",
        "print(\"File has {0} lines, {1} words, {2} characters\".format           #H\n",
        "                             (line_count, word_count, char_count))     #H"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "File has 4 lines, 30 words, 186 characters\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Lab 8: Refactor word_count\n",
        "Rewrite the word-count program from section 8.7 to make it shorter. You may want to look at the string and list operations already discussed, as well as think about different ways to organize the code. You may also want to make the program smarter so that only alphabetic strings (not symbols or punctuation) count as words."
      ],
      "metadata": {
        "id": "nk1hkA9WbhLP"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Create your solution in this cell\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "nTRXJGDoKcal"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Author's solution\n",
        "\n",
        "# File: word_count_refactored.py\n",
        "\"\"\" Reads a file and returns the number of lines, words,\n",
        "    and characters - similar to the UNIX wc utility\n",
        "\"\"\"\n",
        "\n",
        "# initialze counts\n",
        "line_count = 0\n",
        "word_count = 0\n",
        "char_count = 0\n",
        "\n",
        "# open the file\n",
        "with  open('word_count.tst') as infile:\n",
        "    for line in infile:\n",
        "        line_count += 1\n",
        "        char_count += len(line)\n",
        "        word_count += len(line.split())\n",
        "\n",
        "# print the answers using the format() method\n",
        "print(\"File has {0} lines, {1} words, {2} characters\".format(line_count,\n",
        "                                               word_count, char_count))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "oUc4jaylKceF",
        "outputId": "a0fb4988-3700-4370-b1a7-146775684361"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "File has 4 lines, 30 words, 189 characters\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "Generated by Colaboratory\n",
        "\n",
        "# prompt: Refactor the word-count program above to make it shorter.\n",
        "#         You may want to look at the string and list operations, as well as\n",
        "#         think about  different ways to organize the code.\n",
        "\n",
        "#!/usr/bin/env python3\n",
        "\n",
        "''' Reads a file and returns the number of lines, words,\n",
        "    and characters - similar to the UNIX wc utility\n",
        "'''\n",
        "\n",
        "with open('word_count.tst') as infile:\n",
        "    lines = infile.read().split(\"\\n\")\n",
        "    words = [word for line in lines for word in line.split()]\n",
        "    chars = sum(len(line) for line in lines)\n",
        "\n",
        "print(\"File has {0} lines, {1} words, {2} characters\".format(len(lines), len(words), chars))\n"
      ],
      "metadata": {
        "id": "PDcqmi79rMqs",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "56e4bb3e-711d-4eca-f013-577966d60c2b"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "File has 4 lines, 30 words, 186 characters\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Generated by Copilot\n",
        "#!/usr/bin/env python3\n",
        "\n",
        "\"\"\" Reads a file and returns the number of lines, words,\n",
        "    and characters - similar to the UNIX wc utility\n",
        "\"\"\"\n",
        "\n",
        "with open('word_count.tst') as infile:\n",
        "    lines = infile.readlines()\n",
        "\n",
        "line_count = len(lines)\n",
        "word_count = sum(len(line.split()) for line in lines)\n",
        "char_count = sum(len(line) for line in lines)\n",
        "\n",
        "print(f\"File has {line_count} lines, {word_count} words, {char_count} characters\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "WSk1p4XvIVAi",
        "outputId": "763e69aa-e09e-4b3f-922b-5848ef465aeb"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "File has 4 lines, 30 words, 189 characters\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "42bKXQdrKKMy"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}